package com.cloudxingkong.dianyinke.NewTeacher.Adapter;


import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.NewTeacher.Ui.SettingsActivity;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.LoadHomePicUtil;
import com.cloudxingkong.dianyinke.R;

import java.util.HashMap;
import java.util.List;

/**
 * Created by LeeQ on 2016/4/15.
 */
public class HomeLoopAdapter extends PagerAdapter {
    private Context context;
    private List<HashMap<String,String>> data;
    private final int FAKE_DEMO_SIZE=100;

    private ViewPager viewPager;
    public HomeLoopAdapter(ViewPager viewPager,List<HashMap<String, String>> data, Context context) {
        this.data = data;
        this.context = context;
        this.viewPager=viewPager;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        position %= data.size();

        View view = LayoutInflater.from(context).inflate(R.layout.item_home_viewpager, container, false);
        ImageView image = (ImageView) view.findViewById(R.id.item_home_vp_imageview);

        String path = data.get(position).get("path");

        LoadHomePicUtil.loadImage(data.get(position).get("path"), image);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, SettingsActivity.class));
            }
        });
        container.addView(view);

        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        return FAKE_DEMO_SIZE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public void finishUpdate(ViewGroup container) {
        int position=viewPager.getCurrentItem();
        if (position == 0) {
            position=data.size();
            viewPager.setCurrentItem(position,false);
        }else if (position == FAKE_DEMO_SIZE - 1) {
            position=data.size()-1;
            viewPager.setCurrentItem(position);
        }
    }
}
